package org.NooLab.texxplugg.tika;

import java.util.ArrayList;


import org.NooLab.structures.texx.plugg.algo.intf.AlgoReaderAbstract;
import org.NooLab.structures.texx.plugg.algo.intf.AlgoSourceDescription;
import org.NooLab.utilities.logging.CreateLogging;
import org.NooLab.utilities.logging.LogControl;
import org.NooLab.utilities.net.GUID;
import org.apache.commons.collections.BidiMap;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;


// import java.util.logging.Level;


public class TikaReader extends AlgoReaderAbstract{

	private static final long serialVersionUID = -7481313395807452334L;
	
	// private final static Logger log = Logger.getLogger(MIMETypeDetector.class.getName());
	
	String versionStr = "1.00.02" ;
	String receiptGuid = "" ;

	int resultType=0;
	String resultText ="";
	TikaWrapper tika = new TikaWrapper();
	
	// ========================================================================
	public TikaReader(){
		super();
	}
	// ========================================================================
	
	
	@Override
	public String setDataDescription( Object descriptionObj) {
		// 
	
		sourceDescription = (AlgoSourceDescription)descriptionObj;
		
		receiptGuid = GUID.randomvalue() ;
		return receiptGuid;
	}

	private void initLog(){
		 
		CreateLogging.setDebugLevel( 0 ) ; // LogControl.loggerLevel ) ;

		 
		
		LogControl.cLogger.setLogging( "org.apache.tika" );
		LogControl.cLogger.setLogging( "org.apache.tika.parser");
		LogControl.cLogger.setLogging( "org.apache.tika.sax");
		LogControl.cLogger.setLogging( "org.apache.tika" );
		LogControl.cLogger.setLogging( "org.NooLab.texxplugg" );

		Logger lgg = Logger.getRootLogger();
		lgg.setLevel(Level.ERROR);

		lgg.removeAllAppenders() ;
		
		lgg = Logger.getLogger("org.NooLab.texxplugg");
		
		
		// Logger.getLogger(this.getClass().getName()).log(Level.ERROR, null, exception);
		 
	}
	/*
	 org.apache.tika.detect.DefaultDetector;
	 org.apache.tika.detect.Detector;
	 org.apache.tika.io.TikaInputStream;
	 org.apache.tika.metadata.Metadata;
	 org.apache.tika.parser.AutoDetectParser;
	 org.apache.tika.parser.ParseContext;
	 org.apache.tika.parser.Parser;
	 org.apache.tika.sax.BodyContentHandler;
	 */
	/**
	 * 
	 * 
	 */
	@Override
	public int calculate() throws Exception {
		// 
		String xMeta="";
		resultText ="";
		int rt=0;
		
		initLog();
		
		tika.setsource( sourceDescription.getUrlstring() ) ;
		tika.setResultFormat(resultType) ;
		
		tika.perform();

		
		return 0;
	}

	@Override
	public boolean performPostProcessing(){
		boolean rB = true;
		resultText = resultText + " " ;
		/*
		 * in pdf:
		 *    - lines with number at pos 1 -> quite likely a footnote => put to end
		 *    - lines with lots of blanks (1 blank between each char = bold) likely a header of the page
		 *      (if it is repeating => remove
		 *    - mixed small and large chars : likely to be a header / footer => remove  
		 */
		return rB;
	}
	
	@Override
	public void setParameters(ArrayList<Object> params)  throws Exception{
		BidiMap pmap;
		
		if ((params!=null) && (params.size()>0)){
			if (params.get(0) instanceof BidiMap){
				pmap = (BidiMap) params.get(0);
				if (pmap.size()>0){
					if (pmap.containsKey("result")){
						resultType = (Integer) pmap.get("result");
						hasParameters = true;
					}
				}
			}
		}
	}
	
	@Override
	public String getDocTypeMime(){
		// "mime :: AlgoReaderAbstract :: n.i.y. ";
		
		return tika.mimeValue;
	}

	@Override
	public String getText() {
		
		resultText = tika.getText();
		
		return resultText ;
	}

	@Override
	public int getInputSize() {
		
		return -1;
	}

	@Override
	public int getOutputSize() {
		
		return -1;
	}

	@Override
	public String getVersion() {
	
		return versionStr;
	}

	@Override
	public ArrayList<Double> getDescriptiveResults() {
		// 
		return null;
	}

	@Override
	public String getDescription() {
		// 
		return "not available yet";
	}


}
